package com.marchsoft.group.manager.security.handler;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * fetch
 * @Author zhanghaoqi
 * @Description   //登陆失败，返回给前台的数据。    token失效访问接口时？   * Spring security其他异常处理类,比如请求路径不存在等，
 *                                                                        * 如果不配置此类，则Spring security默认会跳转到登录页面
 * @Data 16:53 2020/12/18
 */
@Component
public class JwtAuthenticationEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException authException) throws IOException, ServletException {
        // 当用户尝试访问安全的REST资源而不提供任何凭据时，将调用此方法发送401 响应
        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException == null ? "Unauthorized" :
                authException.getMessage());
    }
}
